import Keycloak from 'keycloak-js';
import KeycloakConfig from '../common/keycloak.json';

export function KeycloakInit() {
  return new Promise((resolve, reject) => {
    const kc = Keycloak(KeycloakConfig);
    kc.init({
      onLoad: 'login-required',
    }).success((authenticated) => {
      resolve({ kc, authenticated });
    }).error((e) => {
      reject(e);
    });
  });
}

export function KeyCloakUser(kc) {
  return new Promise((resolve, reject) => {
    kc.loadUserInfo().success((info) => {
      resolve(info);
    }).error((e) => {
      reject(e);
    });
  });
}
